// SPDX-License-Identifier: Apache-2.0
import { expect, test } from "bun:test";
import {
	FMT_yyyyMMdd,
	YEAR,
	dateTime,
	days,
	defFormat,
	hours,
	months,
	years,
} from "../src/index.js";

test("hours", () => {
	const fmt = defFormat(["dd", " ", "h", ":", "mm", " ", "A"]);
	expect(
		[
			...hours(
				Date.UTC(2019, 11, 30, 15, 1),
				Date.UTC(2019, 11, 31, 15, 1)
			),
		].map((x) => fmt(x, true))
	).toEqual([
		"30 4:00 PM",
		"30 5:00 PM",
		"30 6:00 PM",
		"30 7:00 PM",
		"30 8:00 PM",
		"30 9:00 PM",
		"30 10:00 PM",
		"30 11:00 PM",
		"31 12:00 AM",
		"31 1:00 AM",
		"31 2:00 AM",
		"31 3:00 AM",
		"31 4:00 AM",
		"31 5:00 AM",
		"31 6:00 AM",
		"31 7:00 AM",
		"31 8:00 AM",
		"31 9:00 AM",
		"31 10:00 AM",
		"31 11:00 AM",
		"31 12:00 PM",
		"31 1:00 PM",
		"31 2:00 PM",
		"31 3:00 PM",
	]);
});

test("days", () => {
	expect(
		[...days(Date.UTC(2019, 11, 30, 15), Date.UTC(2020, 1, 1, 1))].map(
			(x) => FMT_yyyyMMdd(x)
		)
	).toEqual([
		"2019-12-31",
		"2020-01-01",
		"2020-01-02",
		"2020-01-03",
		"2020-01-04",
		"2020-01-05",
		"2020-01-06",
		"2020-01-07",
		"2020-01-08",
		"2020-01-09",
		"2020-01-10",
		"2020-01-11",
		"2020-01-12",
		"2020-01-13",
		"2020-01-14",
		"2020-01-15",
		"2020-01-16",
		"2020-01-17",
		"2020-01-18",
		"2020-01-19",
		"2020-01-20",
		"2020-01-21",
		"2020-01-22",
		"2020-01-23",
		"2020-01-24",
		"2020-01-25",
		"2020-01-26",
		"2020-01-27",
		"2020-01-28",
		"2020-01-29",
		"2020-01-30",
		"2020-01-31",
		"2020-02-01",
	]);
});

test("months", () => {
	expect(
		[...months(Date.UTC(2019, 10, 30, 15), Date.UTC(2020, 10, 1, 1))].map(
			(x) => FMT_yyyyMMdd(x)
		)
	).toEqual([
		"2019-12-01",
		"2020-01-01",
		"2020-02-01",
		"2020-03-01",
		"2020-04-01",
		"2020-05-01",
		"2020-06-01",
		"2020-07-01",
		"2020-08-01",
		"2020-09-01",
		"2020-10-01",
		"2020-11-01",
	]);
});

test("years", () => {
	expect(
		[...years(Date.UTC(1996, 10, 30, 15), Date.UTC(2005, 10, 1, 1))].map(
			(x) => FMT_yyyyMMdd(x)
		)
	).toEqual([
		"1997-01-01",
		"1998-01-01",
		"1999-01-01",
		"2000-01-01",
		"2001-01-01",
		"2002-01-01",
		"2003-01-01",
		"2004-01-01",
		"2005-01-01",
	]);
});

test("arg coercion", () => {
	expect(
		[...years(dateTime(0), dateTime(YEAR))].map((x) => FMT_yyyyMMdd(x))
	).toEqual(["1970-01-01", "1971-01-01"]);
	expect(
		[...years(new Date(0), new Date(YEAR))].map((x) => FMT_yyyyMMdd(x))
	).toEqual(["1970-01-01", "1971-01-01"]);
});
